from django.http import HttpResponse, Http404
from sqlalchemy.orm import Query
import meta



def get_entity_or_404(model, *args, **kwargs):
    """
    Similar to django.shortcuts.get_object_or_404.
    TODO: Need to work on sessions here.
    """
    session = kwargs.get('session')
    if session is None:
        # TODO: Should pick only proper, not Global
        session = meta.get_Session()
    entity = session.query(model).get(*args)
    if entity is not None:
        return entity
    else:
        raise Http404('No %s matches the given query.' % model.__name__)
    
